package kingiis.utils.simpleapp.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

/**
 * Created by King on 2018/1/5 0005.
 * fra viewpager 适配器
 */

public class MyFraPagerAdapter extends FragmentPagerAdapter {
    private FragmentTransaction mCurTransaction = null;

    FragmentManager fm;
    ArrayList<Fragment> fras;

    public MyFraPagerAdapter(FragmentManager fm) {
        super(fm);
        this.fm = fm;
    }

    public MyFraPagerAdapter(FragmentManager fm, ArrayList<Fragment> fras) {
        super(fm);
        this.fm = fm;
        this.fras = fras;
    }

    @Override
    public int getCount() {
        return fras.size();
    }


    @Override
    public Fragment getItem(int position) {
        return fras.get(position);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return ((Fragment) object).getView() == view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (mCurTransaction == null) {
            // 创建新事务
            mCurTransaction = fm.beginTransaction();
        }
        // 将Fragment移出UI,但并未从FragmentManager中移除
        mCurTransaction.detach((Fragment) object);
    }
}
